package com.xs.ahoi.hcxs.ui.dialog;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnItemClick;
import com.xs.ahoi.hcxs.Ahoi;
import com.xs.ahoi.hcxs.R;
import com.xs.ahoi.hcxs.dao.ConfigDao;
import com.xs.ahoi.hcxs.service.BGMService;
import com.xs.ahoi.hcxs.utils.FileUtil;
import com.xs.ahoi.hcxs.utils.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author: cdfuhuichao
 * date: 2019/8/12 19:58
 * path: com.xs.ahoi.hcxs.ui.dialog.DialogBGM
 * description: 背景音乐选择列表
 */
public class DialogBGM extends BaseDialog {

    @BindView(R.id.lvMusicList)
    public ListView lvMusicList;
    @BindView(R.id.sbVolumn)
    public SeekBar sbVolumn;

    private ConfigDao configDao;
    private List<Map<String, Object>> datas;
    private Map<String, Object> oneData;
    private DataAdapter adapter;
    private BGMService.BGMBinder bgmControl = Ahoi.getInstance().getBGMControl();

    // 列表数据
    private String[] dataArray = FileUtil.getAssetsFiles("bgm");

    public DialogBGM(@NonNull Context context) {
        super(context, R.style.common_dialog);
    }

    @Override
    protected void onStart() {
        super.onStart();
        initData();
    }

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_bgm;
    }

    @Override
    protected int showPosition() {
        return Gravity.CENTER;
    }

    // 初始化
    @Override
    protected void initData() {
        configDao = ConfigDao.getInstance();

        // 音量变化监听
        sbVolumn.setProgress(configDao.getBgmV());
        sbVolumn.setOnSeekBarChangeListener(seekBarListener);

        // 获取缓存中保存的BGM
        String defaultName = configDao.getBgmName();

        // 初始化列表数据
        datas = new ArrayList<>();
        for (int i = 0; i < dataArray.length; i++) {
            oneData = new HashMap<>();
            oneData.put("name", dataArray[i]);
            oneData.put("select", 0);
            if (defaultName.equals(dataArray[i])) {
                oneData.put("select", 1);
            }
            datas.add(oneData);
        }

        // 绑定adapter
        adapter = new DataAdapter(getContext(), datas, R.layout.listview_item_dialog_bgm, new String[]{"name"}, new int[]{R.id.tvName});
        lvMusicList.setAdapter(adapter);
    }

    @OnItemClick({R.id.lvMusicList})
    public void onItemClick(int position) {

        // 获取点击的章节目录
        Map<String, Object> map;
        for (int i = 0; i < datas.size(); i++) {
            map = datas.get(i);
            map.put("select", 0);
            if (i == position) {
                map.put("select", 1);
            }
        }
        adapter.notifyDataSetChanged();
    }

    @OnClick({R.id.tvDialogOk, R.id.tvDialogCancel})
    public void onClick(View v) {
        switch (v.getId()) {
            // 确认
            case R.id.tvDialogOk:
                change();
                super.dismiss();
                break;
            // 取消
            case R.id.tvDialogCancel:
                super.dismiss();
                break;
        }
    }

    // 获取选中的项
    private String getSelectedItem() {
        Map<String, Object> map;
        for (int i = 0; i < datas.size(); i++) {
            map = datas.get(i);
            if (ObjectUtils.toInteger(map.get("select")) == 1) {
                return map.get("name").toString();
            }
        }
        return "null";
    }

    // 改变结果
    private void change() {
        String selectedName = getSelectedItem();
        String defaultName = configDao.getBgmName();
        if (!selectedName.equals(defaultName)) {
            configDao.setBgmName(selectedName);
            bgmControl.changeS(selectedName);
        }
    }

    // seekbar监听
    SeekBar.OnSeekBarChangeListener seekBarListener = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            bgmControl.changeV(progress);
            configDao.setBgmV(progress);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
    };


    //**********************************************************
    //********************get set 方法**************************
    //**********************************************************

    // 自定义SimpleAdapter
    private class DataAdapter extends SimpleAdapter {

        private DataAdapter(Context context, List<? extends Map<String, Object>> datas, int resource, String[] from, int[] to) {
            super(context, datas, resource, from, to);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);

            // 将已选中的编码设置为红色
            Map<String, Object> data = getItem(position);
            TextView tvName = view.findViewById(R.id.tvName);
            ImageView ivCheck = view.findViewById(R.id.ivCheck);
            if (ObjectUtils.toInteger(data.get("select")) == 1) {
                tvName.setTextColor(ContextCompat.getColor(getContext(), R.color.colorRed));
                ivCheck.setVisibility(View.VISIBLE);
            } else {
                tvName.setTextColor(ContextCompat.getColor(getContext(), R.color.colorBlack));
                ivCheck.setVisibility(View.GONE);
            }
            return view;
        }

        @Override
        public Map<String, Object> getItem(int position) {
            return datas.get(position);
        }
    }
}
